package com.opd.soeghtool;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class SocketRecvService extends Service {
    private final String TAG ="UdpData";

    private final int GET_CONNECT_STATUS = 0x10;

    private final int  GET_INJECT_STATUS =	0x12;

    private final int  GET_SIMULATE_MODE =	0x14;

    private final int  GET_SERVER_VERSION	=		0x1A;
    private final int  GET_SERVER_DATE		=	0x1C;

    private final int  SET_STOP_SERVER	=	0x1E;
    private final int  SET_COMMUNICATION_DATA	=	0x1F;

    private final int GET_RESOLUTION_WIDTH = 0x20;
    private final int GET_RESOLUTION_HEIGHT = 0x22;
    private final int GET_SCREEN_ORIENTATION = 0x24;

    private final int GET_HANDSHAKE_ACK = 0x30;
    GlobalVar mGlobalVar;
    byte[] u8Data;
    UdpSocketHelper udpSocketHelper;

    public byte calcChecksum(byte[] data,int pos, int len)
    {
        byte cks = 0;
        for(int a=0;a<len;a++)
        {
            cks+=data[pos+a];
        }
        cks = (byte)(~cks);
        return cks;
    }


    public void requestHandler()
    {
        byte []data = new byte[32];
        if(u8Data[1]==calcChecksum(u8Data,2,u8Data[0]))
        {
            switch (u8Data[2]&0x7F)
            {
                case GET_CONNECT_STATUS:
                    data[0] = GET_CONNECT_STATUS;
                    if(mGlobalVar.getbIsUsbConnect()) {
                        data[1] = 1;

                    }
                    else {
                        data[1] = 0;
                    }
                    udpSocketHelper.sendCmd(data,2);
                    break;
                case GET_RESOLUTION_WIDTH:
                    data[0] = GET_RESOLUTION_WIDTH;
                    data[1] = (byte)(mGlobalVar.getScreenWidth()&0xff);
                    data[2] = (byte)((mGlobalVar.getScreenWidth()>>8)&0xff);
                    udpSocketHelper.sendCmd(data,3);
                    break;
                case GET_RESOLUTION_HEIGHT:
                    data[0] = GET_RESOLUTION_HEIGHT;
                    data[1] = (byte)(mGlobalVar.getScreenHeight()&0xff);
                    data[2] = (byte)((mGlobalVar.getScreenHeight()>>8)&0xff);
                    udpSocketHelper.sendCmd(data,3);
                    break;
                case GET_SCREEN_ORIENTATION:

                    break;
                case SET_COMMUNICATION_DATA:
                    byte []da = new byte[u8Data[0]-1];
                    System.arraycopy(u8Data,3,da,0,u8Data[0]-1);
                    String str =new String(da);
                    Log.d("OPDHandShake",str);
                     break;
                case GET_SIMULATE_MODE:
                    if(u8Data[3]==1)
                    {
                        mGlobalVar.setIsSimulateMode(true);
                    }
                    else
                    {
                        mGlobalVar.setIsSimulateMode(false);
                    }
                    break;

                case GET_INJECT_STATUS:
                        if(u8Data[3]==1)
                        {
                            mGlobalVar.setIsInjectData(true);
                        }
                        else
                        {
                            mGlobalVar.setIsInjectData(false);
                        }
                        mGlobalVar.setIsInjectServerRunning(true);
                    Log.d(TAG,"Unicorn_Server Start!");
                    break;
                case GET_SERVER_VERSION:
                    break;
                case GET_SERVER_DATE:
                    break;
                     default:
                         break;
            }
        }
        Log.d(TAG,"requestHandler");
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate()
    {
        u8Data = new byte[32];
        mGlobalVar = (GlobalVar)getApplication();
        super.onCreate();
        mGlobalVar.setIsSocketRecvServiceRunning(true);
        udpSocketHelper = new UdpSocketHelper(4000,4400);
        udpSocketHelper.receiveMessage(new UdpSocketHelper.OnRecvListener() {
            @Override
            public void onDataReceive(int len, byte[] data) {
                if((data[0]+2)==len) {
                    System.arraycopy(data, 0, u8Data, 0, len);
                    requestHandler();
                }
            }
        });
        final Handler handler=new Handler();
        Runnable runnable=new Runnable() {
            byte []data = new byte[8];
            @Override
            public void run() {
            // TODO Auto-generated method stub
            //要做的事情
                data[0] = GET_HANDSHAKE_ACK;
                data[1] = 0x1A;
                data[2] = 0x2A;
                data[3] = 0x3A;
                data[4] = 0x4A;
                data[5] = 0x5A;
                udpSocketHelper.sendCmd(data,6);
                Log.d(TAG,"sendCmd...");
                handler.postDelayed(this, 3000);
            }
        };
        handler.postDelayed(runnable, 2000);
    }
}
